<template>
  <div>
    <el-dialog
      :title="addDeptsForm.id?'编辑部门':'增加部门'"
      :visible="visible"
      width="30%"
      @close="onDialogClose"
    >
      <!-- 绑定整个表单数据 -->
      <el-form ref="form" :model="addDeptsForm" :rules="addDeptsFormRules" label-width="100px">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="addDeptsForm.name" placeholder="1-10位字符" />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="addDeptsForm.code" placeholder="1-10位字符" />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <!-- <el-input v-model="addDeptsForm.manager" placeholder="1-10位字符" /> -->
          <el-select v-model="addDeptsForm.manager" placeholder="请选择" @focus="getManagerList">
            <!-- label  要显示到页面上的数据 -->
            <!-- value  页面上的数据的索引 -->
            <el-option
              v-for="item in deptsManager"
              :key="item.id"
              :label="item.username"
              :value="item.username"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="addDeptsForm.introduce" placeholder="1-50位字符" />
        </el-form-item>
      </el-form>

      <!-- 弹出层 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="$emit('input',false)">取 消</el-button>
        <el-button type="primary" @click="onSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getDepartmentsList, addDepartments, getDepartmentsById, editDepartmentsById } from '@/api/department'
import { getDeptsManagerListSimple } from '@/api/employees'
export default {
  model: {
    prop: 'visible'
  },
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    node: {
      type: Object,
      required: true
    }
  },
  data() {
    // 校验部门名称
    const validDateName = async(rule, value, callback) => {
      let flag = false
      if (this.addDeptsForm.id) {
        // 编辑
        // 获取到所有的部门
        const res = await getDepartmentsList()
        // 筛选到同级别的部门，并且排除自己
        const newArr = res.depts.filter(item => item.pid === this.node.pid && item.id !== this.node.id)
        // 查看同级别部门是否有重名
        flag = newArr.some(item => item.name === value)
      } else {
        // 新增
        if (!this.node.children) return callback()
        flag = this.node.children.some(item => item.name === value)
      }

      if (flag) {
        callback(new Error('不能输入部门名称为' + value))
      } else {
        callback()
      }
    }
    // 校验部门编码
    const validDateCode = async(rule, value, callback) => {
      const res = await getDepartmentsList()
      let flag = false
      if (this.addDeptsForm.id) {
        // 如果有id就是编辑，没有就是增加
        const newArr = res.depts.filter(item => item.code !== this.node.code)
        flag = newArr.some(item => item.code === value)
      } else {
        flag = res.depts.some(item => item.code === value)
      }
      // console.log(res)
      if (flag) return callback(new Error('不允许部门编码为' + value))
      callback()
    }
    return {
      // 弹层提交的数据
      addDeptsForm: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      // 规则
      addDeptsFormRules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 10, message: '部门名称长度1-10', trigger: 'blur' },
          { validator: validDateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 1, max: 10, message: '部门编码长度1-10', trigger: 'blur' },
          { validator: validDateCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门负责人', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 50, message: '部门介绍长度1-50', trigger: 'blur' }
        ]
      },
      // 负责人数组
      deptsManager: []
    }
  },

  created() {

  },

  methods: {
    // 对话框关闭
    onDialogClose() {
      this.$emit('input', false)
      // 对表单数据进行重置
      this.$refs.form.resetFields()
      this.addDeptsForm = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
    },
    // 获取员工简单列表
    async  getManagerList() {
      const res = await getDeptsManagerListSimple()
      this.deptsManager = res
    },
    // 确定提交表单要进行校验
    async onSave() {
      // 优化
      try {
        await this.$refs.form.validate()
        if (this.addDeptsForm.id) {
        // 编辑
          await editDepartmentsById(this.addDeptsForm)
          this.$message.success('修改成功')
          this.$emit('edit-success')
        } else {
        // 增加
          // console.log('成功')
          this.addDeptsForm.pid = this.node.id
          await addDepartments(this.addDeptsForm)
          this.$message.success('添加成功')
          this.$emit('add-success')
        }
        this.onDialogClose()
      } catch (err) {
        console.log(err)
      }
      // if (this.addDeptsForm.id) {
      //   // 编辑
      //   try {
      //     await this.$refs.form.validate()
      //     editDepartmentsById(this.addDeptsForm)
      //     this.$emit('edit-success')
      //     this.$message.success('修改成功')
      //     this.onDialogClose()
      //   } catch (err) {
      //     console.log(err)
      //   }
      // } else {
      //   // 增加
      //   try {
      //     await this.$refs.form.validate()
      //     // console.log('成功')
      //     this.addDeptsForm.pid = this.node.id
      //     await addDepartments(this.addDeptsForm)
      //     this.$emit('add-success')
      //     this.$message.success('添加成功')
      //     this.onDialogClose()
      //   } catch (error) {
      //     console.log(error)
      //   }
      // }

      // 原本的写法
    },
    // 得到最新的数据
    async getDeptsInfo(id) {
      console.log('获取最新的数据', id)
      const res = await getDepartmentsById(id)
      this.addDeptsForm = res
    }

  }
}
</script>

<style scoped lang='scss'>

</style>

